﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;
using System.Dynamic;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 小区管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]

    [ApiExplorerSettings(GroupName = "物业")]
    public class CellmanagementController : ControllerBase
    {
        public readonly IFreeSql db;
        public CellmanagementController(IFreeSql db)
        {
            this.db = db;
        }
        [HttpGet]
        public object srech(int page, int pagenum, string? name)
        {
            int pageSize = page; // 每页显示的条目数
            int pageIndex = pagenum; // 当前页码
            var list= db.Select<CellInformation>().ToList();
            if (!string.IsNullOrEmpty(name))
            {
                list =list.Where(a => a.CellName.Contains(name)).ToList();
            }
            int totalCount = list.Count(); // 总条目数
            var item = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var date = item.Select(a => new
            {
                a.CellId,
                a.CellCreateTime,
                a.CellState,
                a.CellCity,
                a.CellGreening,
                a.CellName,
                a.CellHousehold,
                a.CellAddress,
                a.CellArea,
                a.CellDevelopers,
                a.CellProperty,
                a.CellPropertyCompany,
                a.CellProvince,
                a.CellImg
            });
            return new
            {
                TotalCount = totalCount,
                Data = date
            };
        }
        [HttpPost]
        public int Add(CellInformation cellinfo)
        {
            var cell=new CellInformation()
            {
                CellName= cellinfo.CellName,
                CellAddress= cellinfo.CellAddress,
                CellArea= cellinfo.CellArea,
                CellCity= cellinfo.CellCity,
                CellCreateTime= cellinfo.CellCreateTime,
                CellDevelopers= cellinfo.CellDevelopers,
                CellGreening= cellinfo.CellGreening,
                CellHousehold= cellinfo.CellHousehold,
                CellImg= cellinfo.CellImg,
                CellProperty= cellinfo.CellProperty,
                CellProvince= cellinfo.CellProvince,
                CellPropertyCompany= cellinfo.CellPropertyCompany,
                CellState= cellinfo.CellState,
            };
            var query = db.Insert(cell).ExecuteAffrows();
            return query;
        }
        [HttpPut]
        public int Update(int id,CellInformation cellinfo)
        {
            var cell = db.Update<CellInformation>().Set(a => new CellInformation
            {
                CellId= cellinfo.CellId,
                CellName = cellinfo.CellName,
                CellAddress = cellinfo.CellAddress,
                CellArea = cellinfo.CellArea,
                CellCity = cellinfo.CellCity,
                CellCreateTime = cellinfo.CellCreateTime,
                CellDevelopers = cellinfo.CellDevelopers,
                CellGreening = cellinfo.CellGreening,
                CellHousehold = cellinfo.CellHousehold,
                CellImg = cellinfo.CellImg,
                CellProperty = cellinfo.CellProperty,
                CellProvince = cellinfo.CellProvince,
                CellPropertyCompany = cellinfo.CellPropertyCompany,
                CellState = cellinfo.CellState,
            }).Where(a => a.CellId == id).ExecuteAffrows();
            return cell;
        }
        [HttpDelete]
        public int Delete(int id)
        {
            var affectedRows = db.Delete<CellInformation>()
                .Where(a => a.CellId == id)
                .ExecuteAffrows(); //id删除
            return affectedRows;
        }

    }
}
